package Document;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

class Document extends JFrame{
    JTextArea input,show;
    JMenuBar bar;
    JMenu menu;
    JMenuItem itemcopy,itemcut,itemPaste;
    TextListener textListener;
    HandListener handListener;
    Document(){
        init();
        setVisible(true);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void init() {
        input=new JTextArea(15,20);
        show=new JTextArea(15,20);
        show.setLineWrap(true);//自动换行
        bar=new JMenuBar();
        menu=new JMenu("编辑");
        itemcopy=new JMenuItem("复制");
        itemcut=new JMenuItem("剪切");
        itemPaste=new JMenuItem("粘贴");
        itemcopy.setAccelerator(KeyStroke.getKeyStroke('c'));//设置快捷方式
        itemcut.setAccelerator(KeyStroke.getKeyStroke('z'));//设置快捷方式
        itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));//设置快捷方式
        itemcopy.setActionCommand("copy");
        itemcut.setActionCommand("cut");
        itemPaste.setActionCommand("paste");
        menu.add(itemcopy);
        menu.add(itemcut);
        menu.add(itemPaste);
        bar.add(menu);
        setJMenuBar(bar);
        add(new JScrollPane(input));
        add(new JScrollPane(show));
        textListener=new TextListener();
        handListener=new HandListener();
        textListener.setText(input);
        textListener.setOutput(show);
        handListener.setText(input);
        handListener.setOutput(show);
        (input.getDocument()).addDocumentListener(textListener);//向文档设置监视器
        itemcopy.addActionListener(handListener);
        itemcut.addActionListener(handListener);
        itemPaste.addActionListener(handListener);


    }
}
class TextListener implements DocumentListener {
    JTextArea text,output;

    public void setText(JTextArea text) {
        this.text = text;
    }

    public void setOutput(JTextArea output) {
        this.output = output;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        String str=text.getText();
        String regex="[\\s\\d\\p{Punct}]+";
        String [] words=str.split(regex);
        Arrays.sort(words);
        output.setText(null);
        for (int i = 0; i < words.length; i++) {
            output.append(words[i]+",");

        }
    }

}
class HandListener implements ActionListener {
    JTextArea text,output;

    public void setText(JTextArea text) {
        this.text = text;
    }

    public void setOutput(JTextArea output) {
        this.output = output;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String  str=e.getActionCommand();
        if (str.equals("copy"))
            output.copy();
            else if (str.equals("cut"))
                output.cut();
            else if (str.equals("paste"))
                output.paste();
    }
}


public class WindowDocument {
    public static void main(String[] args) {
        Document document=new Document();
        document.setBounds(200,200,500,300);
        document.setTitle("单词排序");
    }
}
